package com.woniuxy.consumer;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.rabbitmq.client.Channel;
import com.woniuxy.entity.InfoLog;
import com.woniuxy.mapper.InfoLogMapper;
import com.woniuxy.mapper.RegisteredMapper;
import com.woniuxy.service.IInfoLogService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author：xurice
 * @date：Created in 2024/4/19 16:53
 * @description：添加日志队列消费者
 * @modified By：
 * @version: $
 */
@Component
public class AddLogConsumer {

    @Autowired
    private InfoLogMapper infoLogService;

    @RabbitListener(queues = "addLogQueue")
    public void receive(String msg, Channel channel, Message message){
        try {
            InfoLog infoLog = JSON.parseObject(msg, InfoLog.class);
            infoLogService.add(infoLog);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}
